package com.cskaoyan.service.impl;

import com.cskaoyan.bean.BaseData;
import com.cskaoyan.bean.bo.CommentParam;
import com.cskaoyan.bean.pojo.MallComment;
import com.cskaoyan.bean.pojo.MallCommentExample;
import com.cskaoyan.mapper.MallCommentMapper;
import com.cskaoyan.service.CommentService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: harveyliu
 * @description: 用来处理评论的业务逻辑
 * @time: 2021/7/9 20:14
 */
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    MallCommentMapper commentMapper;

    /**
     * @Author harvey
     * @description: 用来得到所有的评论响应给页面
     * @Date 2021/7/9 20:11
     * url: http://182.92.235.201:8083/admin/comment/list?page=1&limit=20&userId=1&valueId=2&sort=add_time&order=desc
     */
    @Override
    public BaseData<MallComment> queryCommentsByConditions(CommentParam commentParam) {
        PageHelper.startPage(commentParam.getPage(), commentParam.getLimit());
        MallCommentExample commentExample = new MallCommentExample();
        MallCommentExample.Criteria criteria = commentExample.createCriteria();
        if (commentParam.getUserId() != null) {
            criteria.andUserIdEqualTo(commentParam.getUserId());
        }
        if (commentParam.getValueId() != null) {
            criteria.andValueIdEqualTo(commentParam.getValueId());
        }

        criteria.andDeletedEqualTo(false);
        commentExample.setOrderByClause(commentParam.getSort() + " " + commentParam.getOrder());
        List<MallComment> mallComments = commentMapper.selectByExample(commentExample);

        long total = commentMapper.countByExample(commentExample);

        BaseData<MallComment> mallCommentBaseData = new BaseData<>();
        mallCommentBaseData.setItems(mallComments);
        mallCommentBaseData.setTotal(total);

        return mallCommentBaseData;
    }

    /**
     * @Author harvey
     * @description: 用来实现评论的逻辑删除
     * @Date 2021/7/9 20:31
     * url: http://182.92.235.201:8083/admin/comment/delete
     */
    @Override
    public void deleteCommentById(MallComment mallComment) {
        mallComment.setDeleted(true);
        commentMapper.updateByPrimaryKeySelective(mallComment);
    }
}
